package com.springboot.myweb.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.myweb.mapper.UserMapper;
import com.springboot.myweb.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean addUser(User user) {
        return userMapper.insert(user)>0;
    }

    @Override
    public User check_login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("username",user.getUsername())
                .eq("password",user.getPassword());
        return userMapper.selectOne(wrapper);
    }

    @Override
    public List<User> selectUser() {
        return userMapper.selectList(null);
    }

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("username",username);

        return userMapper.selectOne(wrapper);
    }

    @Override
    @Async //告诉程序这是一个异步任务
    public String sync() {
        try {
            Thread.sleep(3000);
            System.out.println("数据处理中");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
